package com.eascs.datalist.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.cloudFile.entity.CloudDatalist;
import com.eascs.cloudFile.service.WebCloudFileService;
import com.eascs.cloudFile.vo.WebCloudFileVo;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.file.FileTypeConstants;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@RestController
@RequestMapping("/rdatalist")
public class DataListRestController extends BaseRestController {

	private static final Logger logger = LoggerFactory.getLogger(DataListRestController.class);

	@Autowired
	private O2oDataListService o2oDataListService;
	@Reference 
	
	private WebCloudFileService webCloudFileService;

	@Value("${fastdfs.http_storage_server}")
	private String fastdfsUrl;
	
	
	/**
	 * @Title: clouds_view
	 * @Description: 从云文档选择附件信息
	 * @author 张庆奇
	 * @param datalist
	 * @param loanapplyNo
	 * @param loanId
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2017年10月26日 上午11:18:11
	 */
	@RequestMapping("/clouds_view")
	public HtmlRestResult clouds_view(String datalist, String loanapplyNo,
			String loanId) {
		ViewContext.put("datalist", datalist);
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("loanId", loanId);
		logger.info("datalist=={}", datalist);
		logger.info("loanapplyNo=={}", loanapplyNo);
		logger.info("loanId=={}", loanId);
		return HtmlRestResult.popHtmlFragment("/datalist/clouds_view");
	}
	



	/**
	 * @Title: save
	 * @Description: 保存从云文档选择的附件信息
	 * @author 张庆奇
	 * @param datalist
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2017年10月26日 上午11:17:34
	 */
	@RequestMapping("/save")
	public BaseRestResult<String> save(O2oDataListEntity datalist) {

	
		String fileName = datalist.getOriginalName();

		String datalistCode = datalist.getDatalistCode();
		if (StringUtils.isEmpty(fileName)) {
			return BaseRestResult.warn("请选择云文档附件");
		}
		String extendName = fileName.substring(fileName.lastIndexOf(".") + 1);
		User user = UserHolder.getUser();
		Date date = new Date();

		boolean flag = o2oDataListService.isDatalistExsit(datalist.getLoanapplyNo(), datalistCode);

		datalist.setCreatDate(date);
		datalist.setUpdateTime(date);
		datalist.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
		datalist.setCreatId(user.getNo());
		datalist.setCreatOr(user.getCname());
		datalist.setExtendNmae(extendName);
		datalist.setIsDelete(StatusContexts.O2O_NO);
		if (flag) {
			o2oDataListService.saveEntity(datalist);
		} else {

			if (datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BZJDATA)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CONTRACT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_BC_CONTRACT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_GUIDELINE)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_NSFJ)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_JQ_NSFJ)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_PICTURE)
					|| datalistCode.equals(FileTypeConstants.PARTNER_TYPE_EVIDENCE)
					|| datalistCode.equals(FileTypeConstants.BRAND_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.LOAN_CHECK_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_TQHK)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_DCWD)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_CREDITFILING)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_O2OPDR)
					|| datalistCode.equals(FileTypeConstants.DALIST_TYPE_FKZZWJ)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_RPMENT)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_PICTURE)
					|| datalistCode.equals(FileTypeConstants.JXS_PARTNER_TYPE_EVIDENCE)) {
				o2oDataListService.saveEntity(datalist);
			}else {
				
				O2oDataListEntity entity =o2oDataListService.findCode(datalist.getLoanapplyNo(), datalistCode);
				entity.setIsDelete(StatusContexts.O2O_YES);
				entity.setUpdateTime(date);
				// 先隐藏=删除在新增一条数据
				o2oDataListService.updateEntityById(entity);
				
				o2oDataListService.saveEntity(datalist);
			}

		}
		return HtmlRestResult.buildClosestReloadResult();
	}
	
	
	@RequestMapping("/common")
	public BaseRestResult<String> common(String fileName,
			String loanId) {
	
		List<CloudDatalist> ydList=new ArrayList<CloudDatalist>();
		if(StringUtils.isNotEmpty(loanId)){
			WebCloudFileVo vo=new WebCloudFileVo(); 
			vo.setCustomerId(loanId);
			vo.setOriginalName(fileName);
			ydList=webCloudFileService.queryCloudDatas(vo);
		}
		
		ViewContext.put("fastdfsUrl", fastdfsUrl);
		ViewContext.put("ydList", ydList);
		
		 return new HtmlRestResult("datalist/common" );
	}
	


}
